package com.example.demo.sys.service.impl;

import com.example.demo.core.utils.BeanConvertor;
import com.example.demo.sys.dao.UserMapper;
import com.example.demo.sys.dao.UserProfileMapper;
import com.example.demo.sys.entity.User;
import com.example.demo.sys.entity.UserFull;
import com.example.demo.sys.entity.UserProfile;
import com.example.demo.sys.service.UserService;
import com.example.demo.sys.service.bo.UserBO;
import com.example.demo.sys.service.bo.UserFullBO;
import com.example.demo.sys.service.bo.UserProfileBO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Objects;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserProfileMapper userProfileMapper;


    @Override
    public UserBO findById(Long id) {
        User user = userMapper.findById(id);
        return UserBO.convert(user);
    }

    @Override
    public void saveUser(UserBO user) {
        userMapper.saveUser(UserBO.entity(user));
    }

    @Override
    public UserBO login(String userName, String passWord) {
        String digest = DigestUtils.md5DigestAsHex(passWord.getBytes(StandardCharsets.UTF_8));
        User user = userMapper.findByUsernamePassword(userName, digest);
        return UserBO.convert(user);
    }

    @Override
    public UserProfileBO getUserInfo(String id) {
        Long longID = Long.parseLong(id);
        UserProfile profile = userProfileMapper.findByUserID(longID);
        if (profile == null) {
            profile = createProfile(longID);
        }
        return UserProfileBO.convert(profile);
    }

    private UserProfile createProfile(Long userId) {
        User user = userMapper.findById(userId);
        UserProfile newProfile = new UserProfile();
        newProfile.setUserId(userId);
        newProfile.setNickname(user.getUsername());
        newProfile.setCreatedAt(System.currentTimeMillis()/1000);
        userProfileMapper.saveUserProfile(newProfile);
        return newProfile;
    }

	@Override
	public UserProfileBO updateUserProfile(UserProfileBO userProfileBo) {
        userProfileBo.setUpdatedAt(System.currentTimeMillis() / 1000);
		userProfileMapper.updateUserProfile(UserProfileBO.entity(userProfileBo));
		return userProfileBo;
	}

	@Override
	public List<UserFullBO> getAllUsers() {
        List<UserFull> users = userMapper.getAll();
        return BeanConvertor.transfer(users, UserFullBO::new);
	}

    @Override
    public void createUser(UserBO userBO) {
        String password = userBO.getPassword();
        User user = UserBO.entity(userBO);
        user.setPassword(DigestUtils.md5DigestAsHex(password.getBytes(StandardCharsets.UTF_8)));
        user.setCreatedAt(System.currentTimeMillis()/1000);
        userMapper.saveUser(user);
    }

    @Override
    public void updateUser(UserBO userBO) {
        User userExist = userMapper.findById(userBO.getId());
        User user = UserBO.entity(userBO);
        String password = userBO.getPassword();
        if(!Objects.equals(userExist.getPassword(), password)) {
            user.setPassword(DigestUtils.md5DigestAsHex(password.getBytes(StandardCharsets.UTF_8)));
        }
        user.setUpdatedAt(System.currentTimeMillis()/1000);
        userMapper.updateUser(user);
    }

    @Override
    public boolean checkUsernameUnique(UserBO userBO) {
        if(userBO.getId()!=null && userBO.getId()>0) {
            return userMapper.checkUsernameUnique(UserBO.entity(userBO)) == null;
        } else {
            return userMapper.findByUsername(UserBO.entity(userBO)) == null;
        }
    }

    @Override
    public void removeUsers(List<String> ids) {
        userMapper.deleteByIds(ids);
    }
}
